# coding: utf-8
from django.http import HttpResponseRedirect
from pycode import View
from pycode.content.models import Article, ArticleText
from pycode import settings
from pycode.admin import make_navigate

class ArticlesListAdmin(View):
	
	def __init__(self):
		View.__init__(self,
			name = "Articles",
			template = "admin/list_base.html",
			url = "articles/",
			urlpattern = "^articles/$"
		)
		self.root_view = True
		self.title = "Articles"
		self.navigate_items = make_navigate(["Articles"])
		self.add_context["manage_can_add"] = True
	
	def dict_view(self, request, *args, **kwargs):
		return {
			"list_items": [{ "name": a.name, "url": str(a.key()) + "/" } for a in Article.all()]
		}

class ArticleAddAdmin(View):
	
	def __init__(self):
		View.__init__(self,
			name = "Add article",
			template = "admin/articles/edit.html",
			url = "articles/add/",
			urlpattern = "^articles/add/$"
		)
		self.title = "Add article"
		self.navigate_items = make_navigate(["Articles", "Add article"])
	
	def dict_view(self, request, *args, **kwargs):
		out = dict()
		out["langs"] = settings.LANGS
		out["is_add"] = True
		if request.POST:
			# создать Article и сохранить
			article = Article()
			article.name = request.POST["name"]
			article.is_news = (request.POST.get("is_news") == "on")
			article.is_published = (request.POST.get("is_published") == "on")
			article.put()
			# create and store textes by langs
			for lang in settings.LANGS:
				text = ArticleText()
				text.lang = lang
				text.title = request.POST["title_" + lang]
				text.short_text = request.POST["short_text_" + lang]
				text.text = request.POST["full_text_" + lang]
				text.article = article
				text.put()
				# TODO: request.notifications.add("Article added", "success")
			return HttpResponseRedirect("../")
			
		return out

class ArticleEditAdmin(View):
	
	def __init__(self):
		View.__init__(self,
			name = "Edit article",
			template = "admin/articles/edit.html",
			urlpattern = "^articles/(?P<key>[-\w]+)/$"
		)
		self.title = "Edit article"
		self.navigate_items = make_navigate(["Articles", "Edit article"])
	
	def dict_view(self, request, *args, **kwargs):
		key = kwargs.pop("key")
		article = Article.get(key)
		self.navigate_items = make_navigate(["Articles", "Edit article"])
		out = dict()
		out["langs"] = settings.LANGS
		out["article"] = article
		
		if request.POST:
			# создать Article и сохранить
			article.name = request.POST["name"]
			article.is_news = (request.POST.get("is_news") == "on")
			article.is_published = (request.POST.get("is_published") == "on")
			article.put()
			# store textes by langs
			for text in article.textes:
				text.title = request.POST["title_" + text.lang]
				text.short_text = request.POST["short_text_" + text.lang]
				text.text = request.POST["full_text_" + text.lang]
				text.put()
				# TODO: request.notifications.add("Article added", "success")
		
		return out


PUBLIC_VIEWS = [ ArticlesListAdmin, ArticleAddAdmin, ArticleEditAdmin ]
